              package com.java.diagnostics.visualizer.impl.logging;
              
              import com.java.diagnostics.visualizer.impl.util.Messages;
              import java.io.PrintWriter;
              import java.io.StringWriter;
              import java.util.Date;
              import java.util.logging.Formatter;
              import java.util.logging.Handler;
              import java.util.logging.Level;
              import java.util.logging.LogRecord;
              
              public class HTMLFormatter extends Formatter
              {
                private static final String NEWLINE = System.getProperty("line.separator");
              
                private static final String TAIL = "</body>" + NEWLINE + "</html>";
              
                private static final String TRACE = Messages.getString("HTMLFormatter.title");
              
                private static final String H2_CLOSE = "</h2>" + NEWLINE;
              
                private static final String HTML_HEAD_HEAD_BODY_H2 = "<html>" + NEWLINE + "<head>" + NEWLINE + "</head>\n<body>" + NEWLINE + "<h2>";
                private static final String RED_EM = "<em style=\"color:red\">";
                private static final String GREEN_EM = "<em style=\"color:green\">";
                private static final String EM_CLOSE = "</em>";
                private static final String EM = "<em>";
                private static final String TAB = "\t";
                private static final String FOUND_EXCEPTION = "Found exception: ";
                private static final String LINEBREAK = NEWLINE + "<p/>" + NEWLINE;
              
                public String format(LogRecord record)
                {
                  StringBuffer buffer = new StringBuffer();
                  String loggerName = record.getLoggerName();
                  if (loggerName.indexOf('.') > -1) {
                    loggerName = loggerName.substring(loggerName.lastIndexOf('.') + 1);
                  }
                  buffer.append(new Date(record.getMillis()));
                  buffer.append("\t");
              
                  Level level = record.getLevel();
                  if (level.intValue() >= Level.WARNING.intValue()) {
                    buffer.append("<em style=\"color:red\">");
                    buffer.append(level);
                    buffer.append("</em>");
                  } else if (level.intValue() == Level.INFO.intValue()) {
                    buffer.append("<em style=\"color:green\">");
                    buffer.append(level);
                    buffer.append("</em>");
                  } else {
                    buffer.append("<em>");
                    buffer.append(level);
                    buffer.append("</em>");
                  }
              
                  buffer.append("\t");
              
                  buffer.append(
                    String.format("[%4d] %s %s", new Object[] { 
                    Integer.valueOf(record.getThreadID()), 
                    loggerName, 
                    record.getMessage() }));
              
                  buffer.append(LINEBREAK);
                  Throwable ex = record.getThrown();
                  while (ex != null) {
                    buffer.append("<b style=\"color:red\">");
                    buffer.append("Found exception: ");
                    buffer.append("</b>");
                    buffer.append(ex.getMessage());
                    buffer.append(LINEBREAK);
                    buffer.append(getStackTraceAsString(ex));
                    ex = ex.getCause();
                  }
                  return buffer.toString();
                }
              
                public String getHead(Handler arg0)
                {
                  return HTML_HEAD_HEAD_BODY_H2 + TRACE + H2_CLOSE;
                }
              
                public String getTail(Handler arg0)
                {
                  return TAIL;
                }
              
                private String getStackTraceAsString(Throwable exception)
                {
                  StringWriter sw = new StringWriter();
                  exception.printStackTrace(new PrintWriter(sw, true));
                  return sw.toString();
                }
              }

